home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / lib / python2.6 / xdrlib.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2009-11-11  |  9KB  |  292 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''Implements (a subset of) Sun XDR -- eXternal Data Representation.
  5.  
  6. See: RFC 1014
  7.  
  8. '''
  9. import struct
  10.  
  11. try:
  12.     from cStringIO import StringIO as _StringIO
  13. except ImportError:
  14.     from StringIO import StringIO as _StringIO
  15.  
  16. __all__ = [
  17.     'Error',
  18.     'Packer',
  19.     'Unpacker',
  20.     'ConversionError']
  21.  
  22. class Error(Exception):
  23.     '''Exception class for this module. Use:
  24.  
  25.     except xdrlib.Error, var:
  26.         # var has the Error instance for the exception
  27.  
  28.     Public ivars:
  29.         msg -- contains the message
  30.  
  31.     '''
  32.     
  33.     def __init__(self, msg):
  34.         self.msg = msg
  35.  
  36.     
  37.     def __repr__(self):
  38.         return repr(self.msg)
  39.  
  40.     
  41.     def __str__(self):
  42.         return str(self.msg)
  43.  
  44.  
  45.  
  46. class ConversionError(Error):
  47.     pass
  48.  
  49.  
  50. class Packer:
  51.     '''Pack various data representations into a buffer.'''
  52.     
  53.     def __init__(self):
  54.         self.reset()
  55.  
  56.     
  57.     def reset(self):
  58.         self._Packer__buf = _StringIO()
  59.  
  60.     
  61.     def get_buffer(self):
  62.         return self._Packer__buf.getvalue()
  63.  
  64.     get_buf = get_buffer
  65.     
  66.     def pack_uint(self, x):
  67.         self._Packer__buf.write(struct.pack('>L', x))
  68.  
  69.     pack_int = pack_uint
  70.     pack_enum = pack_int
  71.     
  72.     def pack_bool(self, x):
  73.         if x:
  74.             self._Packer__buf.write('\x00\x00\x00\x01')
  75.         else:
  76.             self._Packer__buf.write('\x00\x00\x00\x00')
  77.  
  78.     
  79.     def pack_uhyper(self, x):
  80.         self.pack_uint(x >> 32 & 0xFFFFFFFFL)
  81.         self.pack_uint(x & 0xFFFFFFFFL)
  82.  
  83.     pack_hyper = pack_uhyper
  84.     
  85.     def pack_float(self, x):
  86.         
  87.         try:
  88.             self._Packer__buf.write(struct.pack('>f', x))
  89.         except struct.error:
  90.             msg = None
  91.             raise ConversionError, msg
  92.  
  93.  
  94.     
  95.     def pack_double(self, x):
  96.         
  97.         try:
  98.             self._Packer__buf.write(struct.pack('>d', x))
  99.         except struct.error:
  100.             msg = None
  101.             raise ConversionError, msg
  102.  
  103.  
  104.     
  105.     def pack_fstring(self, n, s):
  106.         if n < 0:
  107.             raise ValueError, 'fstring size must be nonnegative'
  108.         n < 0
  109.         data = s[:n]
  110.         n = ((n + 3) // 4) * 4
  111.         data = data + (n - len(data)) * '\x00'
  112.         self._Packer__buf.write(data)
  113.  
  114.     pack_fopaque = pack_fstring
  115.     
  116.     def pack_string(self, s):
  117.         n = len(s)
  118.         self.pack_uint(n)
  119.         self.pack_fstring(n, s)
  120.  
  121.     pack_opaque = pack_string
  122.     pack_bytes = pack_string
  123.     
  124.     def pack_list(self, list, pack_item):
  125.         for item in list:
  126.             self.pack_uint(1)
  127.             pack_item(item)
  128.         
  129.         self.pack_uint(0)
  130.  
  131.     
  132.     def pack_farray(self, n, list, pack_item):
  133.         if len(list) != n:
  134.             raise ValueError, 'wrong array size'
  135.         len(list) != n
  136.         for item in list:
  137.             pack_item(item)
  138.         
  139.  
  140.     
  141.     def pack_array(self, list, pack_item):
  142.         n = len(list)
  143.         self.pack_uint(n)
  144.         self.pack_farray(n, list, pack_item)
  145.  
  146.  
  147.  
  148. class Unpacker:
  149.     '''Unpacks various data representations from the given buffer.'''
  150.     
  151.     def __init__(self, data):
  152.         self.reset(data)
  153.  
  154.     
  155.     def reset(self, data):
  156.         self._Unpacker__buf = data
  157.         self._Unpacker__pos = 0
  158.  
  159.     
  160.     def get_position(self):
  161.         return self._Unpacker__pos
  162.  
  163.     
  164.     def set_position(self, position):
  165.         self._Unpacker__pos = position
  166.  
  167.     
  168.     def get_buffer(self):
  169.         return self._Unpacker__buf
  170.  
  171.     
  172.     def done(self):
  173.         if self._Unpacker__pos < len(self._Unpacker__buf):
  174.             raise Error('unextracted data remains')
  175.         self._Unpacker__pos < len(self._Unpacker__buf)
  176.  
  177.     
  178.     def unpack_uint(self):
  179.         i = self._Unpacker__pos
  180.         self._Unpacker__pos = j = i + 4
  181.         data = self._Unpacker__buf[i:j]
  182.         if len(data) < 4:
  183.             raise EOFError
  184.         len(data) < 4
  185.         x = struct.unpack('>L', data)[0]
  186.         
  187.         try:
  188.             return int(x)
  189.         except OverflowError:
  190.             return x
  191.  
  192.  
  193.     
  194.     def unpack_int(self):
  195.         i = self._Unpacker__pos
  196.         self._Unpacker__pos = j = i + 4
  197.         data = self._Unpacker__buf[i:j]
  198.         if len(data) < 4:
  199.             raise EOFError
  200.         len(data) < 4
  201.         return struct.unpack('>l', data)[0]
  202.  
  203.     unpack_enum = unpack_int
  204.     
  205.     def unpack_bool(self):
  206.         return bool(self.unpack_int())
  207.  
  208.     
  209.     def unpack_uhyper(self):
  210.         hi = self.unpack_uint()
  211.         lo = self.unpack_uint()
  212.         return long(hi) << 32 | lo
  213.  
  214.     
  215.     def unpack_hyper(self):
  216.         x = self.unpack_uhyper()
  217.         if x >= 0x8000000000000000L:
  218.             x = x - 0x10000000000000000L
  219.         
  220.         return x
  221.  
  222.     
  223.     def unpack_float(self):
  224.         i = self._Unpacker__pos
  225.         self._Unpacker__pos = j = i + 4
  226.         data = self._Unpacker__buf[i:j]
  227.         if len(data) < 4:
  228.             raise EOFError
  229.         len(data) < 4
  230.         return struct.unpack('>f', data)[0]
  231.  
  232.     
  233.     def unpack_double(self):
  234.         i = self._Unpacker__pos
  235.         self._Unpacker__pos = j = i + 8
  236.         data = self._Unpacker__buf[i:j]
  237.         if len(data) < 8:
  238.             raise EOFError
  239.         len(data) < 8
  240.         return struct.unpack('>d', data)[0]
  241.  
  242.     
  243.     def unpack_fstring(self, n):
  244.         if n < 0:
  245.             raise ValueError, 'fstring size must be nonnegative'
  246.         n < 0
  247.         i = self._Unpacker__pos
  248.         j = i + ((n + 3) // 4) * 4
  249.         if j > len(self._Unpacker__buf):
  250.             raise EOFError
  251.         j > len(self._Unpacker__buf)
  252.         self._Unpacker__pos = j
  253.         return self._Unpacker__buf[i:i + n]
  254.  
  255.     unpack_fopaque = unpack_fstring
  256.     
  257.     def unpack_string(self):
  258.         n = self.unpack_uint()
  259.         return self.unpack_fstring(n)
  260.  
  261.     unpack_opaque = unpack_string
  262.     unpack_bytes = unpack_string
  263.     
  264.     def unpack_list(self, unpack_item):
  265.         list = []
  266.         while None:
  267.             x = self.unpack_uint()
  268.             if x == 0:
  269.                 break
  270.             
  271.             if x != 1:
  272.                 raise ConversionError, '0 or 1 expected, got %r' % (x,)
  273.             item = unpack_item()
  274.             list.append(item)
  275.             continue
  276.             return list
  277.  
  278.     
  279.     def unpack_farray(self, n, unpack_item):
  280.         list = []
  281.         for i in range(n):
  282.             list.append(unpack_item())
  283.         
  284.         return list
  285.  
  286.     
  287.     def unpack_array(self, unpack_item):
  288.         n = self.unpack_uint()
  289.         return self.unpack_farray(n, unpack_item)
  290.  
  291.  
  292.